require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_MemberPointsSystem_test < Test::Unit::TestCase
  
     #This test is giving error for calculation.
    def startup
    @@ie = Watir::IE.start($SITE_URL)
      end
  
    
    def ie
        if defined? @@ie
          @@ie
        else
         startup
         end
      end

    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
      end

    def test_02_verifyLoginLinksExist
        if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
         end
      end
    
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
      end
      
 
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
      end
      
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
      end

    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
      end
 
    def test_08_navigateToSiteAdministration
        ie.goto($SITE_URL + 'controlpanel/Settings/default.aspx')
        assert(ie.contains_text('Site Administration'))      
      end
      
    def test_09_verifyLinksExist
        # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end
      

    def test_10_navigateToManageMemberPointsSystem
        ie.goto($SITE_URL + 'controlpanel/tools/PointSystem.aspx')
      end
      

    def test_11_navigateToGeneral
        assert(ie.radio(:id, /EnablePointSystem_0$/).exists?)
        assert(ie.radio(:id, /EnablePointSystem_1$/).exists?)
        ie.radio(:id, /EnablePointSystem_0$/).set
        ie.radio(:id, /EnablePointSystem_1$/).clear
        assert(ie.select_list(:id, /PointsPostDisplayLevel$/).exists?)
        ie.select_list(:id, /PointsPostDisplayLevel$/).set('Show only to Administrators and Moderators')
        assert(ie.select_list(:id, /PointsUserDisplayLevel$/).exists?)
        ie.select_list(:id, /PointsUserDisplayLevel$/).set('Show only to Administrators and Moderators')
      end
      

    def test_12_navigateToFactorValues
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.GetTabAtIndex(1))")
        sleep 5
        assert(ie.text_field(:id, /PointsPostFactor$/).exists?)
        ie.text_field(:id, /PointsPostFactor$/).value='6'
        #ie.text_field(:id, /PictureListingColumns$/).value='6'
        assert(ie.text_field(:id, /PointsReplyFactor$/).exists?)
        ie.text_field(:id, /PointsReplyFactor$/).value='15'
        assert(ie.text_field(:id, /PointsReplierFactor$/).exists?)
        ie.text_field(:id, /PointsReplierFactor$/).value='5'
        assert(ie.text_field(:id, /PointsRatingFactor$/).exists?)
        ie.text_field(:id, /PointsRatingFactor$/).value ='7'
        assert(ie.text_field(:id, /PointsDownloadFactor$/).exists?)
        ie.text_field(:id, /PointsDownloadFactor$/).value='0'
        assert(ie.text_field(:id, /PointsDownloaderFactor$/).exists?)
        ie.text_field(:id, /PointsDownloaderFactor$/).value='0'
        assert(ie.text_field(:id, /PointsFavoritePostFactor$/).exists?)
        ie.text_field(:id, /PointsFavoritePostFactor$/).value='10'
        assert(ie.text_field(:id, /PointsFavoriteUserFactor$/).exists?)
        ie.text_field(:id, /PointsFavoriteUserFactor$/).value='50'
        assert(ie.text_field(:id, /PointsRaterFactor$/).exists?)
        ie.text_field(:id, /PointsRaterFactor$/).value='5'
      end
      
    def test_13_Calculate
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.GetTabAtIndex(2))")
        assert(ie.button(:id, /CalculateButton$/).exists?)
        ie.button(:id, /CalculateButton$/).click      
        assert(ie.link(:id,/SaveButton$/).exists?)
        #ie.link(:id,/SaveButton$/).click
      end
      
    def test_99_closeIE
         ie.close
       end

end 